package panda.android.notebook.popupWin;

import panda.android.notebook.R;
import panda.android.notebook.Utils.Utils;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class PopupWin {

	public interface ItemClick {
		public void itemclick(String tag);
	}

	private ItemClick itemClick;
	private PopupWindow popupWindow;

	private String mStrings[] = null;
	private Activity mActivity;

	private boolean isShow = false;

	public void setItemClick(ItemClick itemClick) {
		this.itemClick = itemClick;
	}

	public PopupWin(Activity activity, String strings[]) {
		// TODO Auto-generated constructor stub
		mActivity = activity;
		mStrings = strings;
	}

	public void show(View v) {
		// TODO Auto-generated method stub
		if (isShow) {
			dismiss();
			return;
		}

		ListView listView = new ListView(mActivity);
		listView.setBackgroundResource(R.drawable.button_white);
		listView.setCacheColorHint(Color.TRANSPARENT);
		listView.setSelector(R.drawable.transparent);
		listView.setOnItemClickListener(clickListener);
		listView.setAdapter(new listAdapter());

		Utils utils = new Utils();
		popupWindow = new PopupWindow(listView, utils.width(80),
				utils.height(100)) {
			@Override
			public void dismiss() {
				// TODO Auto-generated method stub
				super.dismiss();
				isShow = false;
			}
		};
		popupWindow.setOutsideTouchable(true);
		popupWindow.setFocusable(true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow.showAsDropDown(v);
		isShow = true;
	}

	OnItemClickListener clickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			itemClick.itemclick(mStrings[position]);
			dismiss();
		}

	};

	public void dismiss() {
		popupWindow.dismiss();
		isShow = false;
	}
	
	class listAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mStrings.length;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mStrings[position];
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			TextView textView = new TextView(mActivity);
			Utils utils = new Utils();
			textView.setText(mStrings[position]);
			textView.setTextColor(Color.BLACK);
			textView.setTextSize(utils.fontsize(12));
			return textView;
		}
		
	}
}
